package org.linys.action.dict;

import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.linys.action.BaseAction;
import org.linys.model.dict.Dictionary;
import org.linys.model.system.Operator;
import org.linys.service.dict.DictionaryServ;
import org.linys.util.PageUtil;
import org.linys.util.ResponseMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings({  "unchecked" })
@Controller("CityTypeAction")
// 要与Struts.xml文件中class的属性相同
@Scope("prototype")
// 多例：每个请求一个action
public class CityTypeAction extends BaseAction implements ModelDriven{
	/**
	 * 
	 */
	private static final long serialVersionUID = -895812679979527981L;

	private Dictionary dictionary = new Dictionary();

	private DictionaryServ dictionaryServ;
	private PageUtil pageUtil;
	public DictionaryServ getDictionaryServ() {
		return dictionaryServ;
	}
	@Autowired
	public void setDictionaryServ(DictionaryServ dictionaryServ) {
		this.dictionaryServ = dictionaryServ;
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}
	@Autowired
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	
	public ResponseMsgUtil getResponseMsgUtil() {
		return responseMsgUtil;
	}
	@Autowired
	public void setResponseMsgUtil(ResponseMsgUtil responseMsgUtil) {
		this.responseMsgUtil = responseMsgUtil;
	}

	private ResponseMsgUtil responseMsgUtil;
	public Object getModel() {
		return dictionary;
	}
	
	/**
	 * 浏览用户列表
	 */
	public void view() {
		try{
			String rows = request.getParameter("rows");
			String page = request.getParameter("page");
			this.dictionary.setType("CityType");
			this.pageUtil.cal(dictionaryServ.count(dictionary), rows, page);

			List<Dictionary> dictonaryList = dictionaryServ.list(dictionary,
					this.pageUtil.getStart(), this.pageUtil.getRows());

			JSONArray jsonArray = JSONArray.fromObject(dictonaryList);
			JSONObject object = new JSONObject();
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
			this.responseMsgUtil.doResponse(object.toString());
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 更新
	 */
	public void update() {
		try{
			Operator operator=(Operator)request.getSession().getAttribute("operator_session");
			dictionary.setOperatorID(operator.getOperatorID_());
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");  
			java.util.Date date=new java.util.Date();  
			String str=sdf.format(date);  
			dictionary.setOperateTime(str);
			 dictionaryServ.update(dictionary);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 插入
	 */
	public void insert() {
		try{
			Operator operator=(Operator)request.getSession().getAttribute("operator_session");
			dictionary.setHandlerID(operator.getOperatorID_());
			dictionary.setOperatorID(operator.getOperatorID_());
			dictionary.setType("CityType");
			int id=dictionaryServ.insert(dictionary);
			JSONObject object = new JSONObject();
			object.put("success", true);
			object.put("id", id);
			this.responseMsgUtil.doResponse(object.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

}
